<?php

namespace App\Http\Controllers\Mgt;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Redis;

use App\Repositories\Eloquent\Mgt\BookRepository as Book;
use DB;

class BookController extends Controller
{
    protected $book;

    public function __construct(Book $book)
    {
        parent::__construct();

        $this->book = $book;
    }


    public function index(Request $request)
    {
        $query = $request->query('query');

        $books = $this->book->index($query)->paginate(60);              

        return view($this->view, compact('books', 'query'));
    }

    
    /**
     * 获取书籍的借阅记录
     *
     * @return \Illuminate\Http\Response
     */
    public function borrows($id)
    {
        $book = $this->book->show($id);

        return view($this->view, compact('book'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = $this->book->categories();

        return view($this->view, compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->book->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $book = $this->book->show($id);

        if ($book instanceof JsonResponse && $book->getStatusCode() === 404) {
            return abort('404');
        }

        $categories = $this->book->categories();

        return view($this->view, compact('book', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->book->renew($request, $id);
    }

    
    public function bookstore()
    {
        return $this->book->bookstore();
    }
}
